#include <fstream>
#include <iostream>
#include <iomanip>

#include "instr_printer.hpp"
#include "opcode_wrapper.hpp"


InstrPrinter::InstrPrinter(std::string outfile_name) : outfile_name_{outfile_name}
{
    // out_stream_ << ".start\n";
}

InstrPrinter::~InstrPrinter()
{
    // out_stream_ << ".end\n";
    to_file(outfile_name_);
}

void InstrPrinter::add_instr(std::string opcode)
{
    out_stream_ << ".word " << opcode_wrapper<OpcodeFmt::Hex>(opcode) << " \\n\\t\\\n";
}

void InstrPrinter::add_instr(std::string opcode, std::string instr_name="")
{
    out_stream_ << std::left << std::setw(20) << \
    instr_name << opcode_wrapper<OpcodeFmt::Binary>(opcode) << '\n';
}

void InstrPrinter::to_file(std::string file_name)
{
    std::ofstream outfile{file_name};
    outfile << out_stream_.rdbuf()->str();
}

void InstrPrinter::to_stdout()
{
    std::cout << out_stream_.rdbuf()->str();
}
